package com.his.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.bean.DzmHisDictionary;
import com.his.bean.DzmHisDoctor;
import com.his.bean.DzmHisMember;
import com.his.mapper.DzmHisMemberMapper;
import com.his.service.DzmHisDoctorService;
import com.his.mapper.DzmHisDoctorMapper;
import net.sf.jsqlparser.expression.StringValue;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 *
 */
@Service

public class DzmHisDoctorServiceImpl extends ServiceImpl<DzmHisDoctorMapper, DzmHisDoctor>
        implements DzmHisDoctorService{

    @Autowired
    DzmHisDoctorMapper dzmHisDoctorMapper;//医生信息

    @Autowired
    DzmHisMemberMapper dzmHisMemberMapper;//用户信息

    @Override
    public List<DzmHisDoctor> getlist(DzmHisDoctor dzmHisDoctor) {
        return dzmHisDoctorMapper.getlist(dzmHisDoctor);
    }

    @Override
    public DzmHisDoctor selectDoctorId(String id) {
        return dzmHisDoctorMapper.selectDoctorId(id);
    }

    /**
     *
     * @param dzmHisDoctor
     * @return
     * 修改医生信息以及医生的用户信息
     */
    @Transactional
    @Override
    public int updateDoctor(DzmHisDoctor dzmHisDoctor) {
        String id = dzmHisDoctor.getId()+"";
        List<DzmHisMember> dzmHisMemberList = dzmHisDoctor.getDzmHisMemberList();
        DzmHisMember dzmHisMember = dzmHisMemberList.get(0);
        String uid = dzmHisMemberList.get(0).getUid()+"";
        //判断医生id和用户id是否为空如果为空 则返回0
        if((uid == null && uid == "") || (id == null && id == "")){
            System.err.println("数据缺失");
            return 0;
        }
        System.err.println("数据完整");
        //修改医生信息
        int doctornum = dzmHisDoctorMapper.updateDoctor(dzmHisDoctor);
        // 如果返回值小于等于0 表示修改失败 抛出异常 Transactional捕获异常并rollback
        //修改用户信息
        int membernum = dzmHisMemberMapper.updateMember(dzmHisMember);
        //如果返回值小于等于0 表示修改失败 抛出异常 Transactional捕获异常并rollback
        if(membernum <=0 || doctornum <= 0){
            throw new RuntimeException();
        }
        return 1;
    }

    /**
     * 添加医生信息以及用户信息
     * @param dzmHisDoctor
     * @return
     */
    @Transactional
    @Override
    public int addDoctor(DzmHisDoctor dzmHisDoctor) {
        List<DzmHisMember> dzmHisMemberList = dzmHisDoctor.getDzmHisMemberList();
        DzmHisMember dzmHisMember = dzmHisMemberList.get(0);
        System.err.println(dzmHisMember);

        //添加用户信息
        int membernum = dzmHisMemberMapper.addMember(dzmHisMember);

        //查出uid最大的把uid放入添加医生的uid字段中
        DzmHisMember hisMember = dzmHisMemberMapper.selectUid();
        dzmHisDoctor.setUid(hisMember.getUid());

        //添加医生信息
        int doctornum = dzmHisDoctorMapper.addDoctor(dzmHisDoctor);
        // 如果返回值小于等于0 表示添加失败 抛出异常 Transactional捕获异常并rollback

        //如果返回值小于等于0 表示添加失败 抛出异常 Transactional捕获异常并rollback
        if(membernum <=0 || doctornum <= 0){
            throw new RuntimeException();
        }
        return 1;
    }

    @Override
    public DzmHisDoctor selectByUid(String uid) {
        return dzmHisDoctorMapper.selectByUid(uid);
    }
}
